﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Transport
{
    public class BangGiaNgayAdminControl
    {
        transportEntities db;

        public BangGiaNgayAdminControl()
        {
            db = new transportEntities();
        }

        public List<bang_gia_trong_ngay> listAll()
        {
            try
            {
                return db.bang_gia_trong_ngay.ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<bang_gia_trong_ngay> listByService(string serviceid)
        {
            try
            {
                return db.bang_gia_trong_ngay.Where(t => t.ma_dich_vu == serviceid).ToList();
            }
            catch
            {
                return null;
            }
        }

        public List<bang_gia_trong_ngay> findByCarNumber(string number)
        {
            try
            {
                return db.bang_gia_trong_ngay.Where(t => t.bien_so == number).ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<bang_gia_trong_ngay> findByServiceID(string serviceid)
        {
            try
            {
                return db.bang_gia_trong_ngay.Where(t => t.ma_dich_vu == serviceid).ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<bang_gia_trong_ngay> findByServiceName(string name)
        {
            try
            {
                var result = (from a in db.bang_gia_trong_ngay
                              from b in db.dich_vu
                              where a.ma_dich_vu == b.id_dich_vu && b.ten_dich_vu == name
                              select a).ToList();

                return result;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public bang_gia_trong_ngay findByCarNumberAndServiceID(string carnumber, string serviceid)
        {
            try
            {
                return db.bang_gia_trong_ngay.SingleOrDefault(t => t.bien_so == carnumber && t.ma_dich_vu == serviceid);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public bool insert(bang_gia_trong_ngay value)
        {
            try
            {
                db.bang_gia_trong_ngay.AddObject(value);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool update(bang_gia_trong_ngay value)
        {
            try
            {
                bang_gia_trong_ngay bg = db.bang_gia_trong_ngay.SingleOrDefault(t => t.bien_so == value.bien_so && t.ma_dich_vu == value.ma_dich_vu);
                bg = value;
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool delete(string carnumber, string serviceid)
        {
            try
            {
                bang_gia_trong_ngay bg = db.bang_gia_trong_ngay.SingleOrDefault(t => t.bien_so == carnumber && t.ma_dich_vu == serviceid);
                db.bang_gia_trong_ngay.DeleteObject(bg);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}